package com.wizz.trade.information.controller;

import com.wizz.trade.common.PageVo;
import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.information.dto.MoneytypeDto;
import com.wizz.trade.information.dto.page.MoneytypePageDto;
import com.wizz.trade.information.entity.Moneytype;
import com.wizz.trade.information.service.MoneytypeService;
import com.wizz.trade.information.vo.MoneytypeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 资料模块/款项类别
 */
@RestController
@RequestMapping("/moneytype")
public class MoneytypeController {


    @Resource
    private MoneytypeService moneytypeService;

    /**
     * 添加操作
     *
     * @param moneytypeDto
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('设置币种、订单模式')")

    public ResponseResult addItem(@RequestBody MoneytypeDto moneytypeDto) {
        Moneytype moneytype = new Moneytype();
        BeanUtils.copyProperties(moneytypeDto, moneytype);
        moneytypeService.save(moneytype);
        return new ResponseResult<>(StatusCode.OK, "添加成功");
    }

    /**
     * 更新（编辑）操作
     *
     * @param moneytypeDto
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('设置币种、订单模式')")

    public ResponseResult updateItem(@RequestBody MoneytypeDto moneytypeDto, @PathVariable("id") Integer id) {
        Moneytype moneytype = new Moneytype();
        BeanUtils.copyProperties(moneytypeDto, moneytype);
        moneytype.setId(id);
        moneytypeService.updateById(moneytype);
        return new ResponseResult<>(StatusCode.OK, "修改成功");
    }

    /**
     * 删除（撤销）操作
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('设置币种、订单模式')")

    public ResponseResult deleteItem(@PathVariable("id") Integer id) {
        moneytypeService.removeById(id);
        return new ResponseResult<>(StatusCode.OK, "删除成功");
    }

    /**
     * 分页刷新、条件查询
     *
     * @param moneytypePageDto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('设置币种、订单模式')")

    public ResponseResult<PageVo<MoneytypeVo>> list(MoneytypePageDto moneytypePageDto) {
        PageVo<Moneytype> rawPage = moneytypeService.getPage(moneytypePageDto);
        List<Moneytype> data = rawPage.getData();
        List<MoneytypeVo> dataVo = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            MoneytypeVo moneytypeVo = new MoneytypeVo();
            BeanUtils.copyProperties(data.get(i), moneytypeVo);
            dataVo.add(moneytypeVo);
        }
        return new ResponseResult<>(StatusCode.OK, new PageVo<>(rawPage.getPages(), rawPage.getTotal(), dataVo));
    }

    /**
     * 下拉框获取款项种类
     * @return 款项种类号
     */
    @GetMapping("/money")
    public ResponseResult<List<Integer>> getMoneyOption(){
        List<Integer> moneyOption = moneytypeService.getMoneyOption();
        return new ResponseResult<>(StatusCode.OK,"测试成功", moneyOption);
    }

    /**
     * 下拉框获取款项明细
     * @param money 款项种类号
     * @return Map<id, moneyType>
     */
    @GetMapping("/type")
    public ResponseResult<List<Map<Integer, String>>> getTypeOption(Integer money){
        List<Map<Integer, String>> typeOption = moneytypeService.getTypeOption(money);
        return new ResponseResult<>(StatusCode.OK,"测试成功", typeOption);
    }

}
